<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="/easyui.css">
    <link rel="stylesheet" type="text/css" href="/icon.css">
    <link rel="stylesheet" type="text/css" href="/demo.css">
    <script src="/jquery.min.js"></script>
    <script src="/jquery.easyui.min.js"></script>
    <meta charset="UTF-8">
    <title>老师班级管理</title>
</head>
<body>
<div>
    <h2>老师列表：</h2>
    <table id="teacher"></table>
    <h2>课程列表：</h2>
    <button id="button" value="分配">给班级分配学生</button>
    <table id="banji"></table>
</div>
<script type="text/javascript">
    $("#teacher").datagrid({
        url:"/TeacherBanjiController/teacherDate",
        singleSelect: true,
        rownumbers: "true",
        fitColumns: "true",
        pagination: "true",
        pageSize: 10,
        columns: [[
            {field: 'tid', title: '编号', width: 100},
            {field: 'tname', title: '名称', width: 100},
            {field: 'tsex', title: '教师性别', width: 100}
        ]]
    })
    var teacherbanji =[];
    $("#banji").datagrid({
        url:'/TeacherBanjiController/banjiDate',
        rownumber:'true',
        fitColumns:'true',
        pagination:"true",
        pageSize:10,
        columns:[[
            {field:'bid',title:'班级号',with:100},
            {field:'bname',title:'班级名字',width:100}
        ]],
        onLoadSuccess:function(data){
            $.each(data.rows,function(index,value){
                $.each(teacherbanji,function(j,v){
                    if(value.bid == v.bid){
                        $("#banji").datagrid('selectRow',index);
                    }
                });
            });
        }
    });
    var tid = "";
    $("#teacher").datagrid({
        onClickRow:function(rowIndex, rowData){
            tid = rowData.tid;
            $.post('/TeacherBanjiController/teacherShowBanji',{tid:rowData.tid},function (data) {
                console.log(data);
                teacherbanji = data;
                $("#banji").datagrid('reload');
            });
        }
    });
    var str = [];
    $("#button").click(function () {
//        获取表格中被选中的多行数据
        rows = $("#banji").datagrid('getSelections');
        $.each(rows,function (i,v) {
            str[i] = rows[i].bid;
        });
        console.log(str);
        $.post("/TeacherBanjiController/allSelectedBanji",{tid:tid,str:str});
    });
    //分页
    function pagerFilter(data){
        if (typeof data.length == 'number' && typeof data.splice == 'function'){	// is array
            data = {
                total: data.length,
                rows: data
            }
        }
        var dg = $(this);
        var opts = dg.datagrid('options');
        var pager = dg.datagrid('getPager');
        pager.pagination({
            onSelectPage:function(pageNum, pageSize){
                opts.pageNumber = pageNum;
                opts.pageSize = pageSize;
                pager.pagination('refresh',{
                    pageNumber:pageNum,
                    pageSize:pageSize
                });
                dg.datagrid('loadData',data);
            }
        });
        if (!data.originalRows){
            data.originalRows = (data.rows);
        }
        var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
        var end = start + parseInt(opts.pageSize);
        data.rows = (data.originalRows.slice(start, end));
        return data;
    }
    $(function(){
        $('#teacher').datagrid({loadFilter:pagerFilter});
    });
    $(function () {
        $("#banji").datagrid({loadFilter:pagerFilter})
    })
</script>

</body>
</html>